<script setup lang="ts" name="SystemOAuth2Token">
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { getIntDictOptions } from '@/utils/dict'
import { DICT_TYPE } from '@/enums/dictEnums'
import { dateTimeFormatter } from '@/utils/dateUtil'
import { OAuth2TokenApi } from '@/api/system/oauth2/token'
import { checkPermit, computedOperateWidth } from '@/utils/permit'
const appStore = useAppStoreWithOut()
const { systemConfig } = storeToRefs(appStore)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const total = ref(0) // 列表的总页数
const list = ref([]) // 表格数据
const queryLoading = ref(false) // 列表加载中：1）列表数据加载；2）查询按钮禁用；3）重置按钮禁用
const queryParams = ref({
  pageIndex: 1,
  pageSize: 20,
  userId: '',
  userType: undefined,
  clientId: ''
})
const queryFormRef = ref() // 搜索的表单


/** 查询列表 */
const getList = async () => {
  queryLoading.value = true
  try {
    const data = await OAuth2TokenApi.page(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    queryLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageIndex = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.pageIndex = 1
  queryParams.value.pageSize = 20
  queryParams.value.userId = ''
  queryParams.value.userType = undefined
  queryParams.value.clientId = ''
  handleQuery()
}

/** 强制退出操作 */
const handleForceLogout = async (accessToken: string) => {
  try {
    // 删除的二次确认
    await message.confirm(t('sys.oauth2.token', 'confirm'))
    // 发起删除
    await OAuth2TokenApi.delete(accessToken)
    message.success(t('message', 'success'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<template>
  <PageCard>
    <ContentFormTable>
      <template #header>
        <ElForm ref="queryFormRef" :model="queryParams" :inline="true">
          <ElFormItem :label="t('sys.oauth2.token', 'userId')" prop="userId" class="!w-240px">
            <el-input v-model="queryParams.userId" :placeholder="t('sys.oauth2.token.rules', 'userId')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem :label="t('sys.oauth2.token', 'userType')" prop="userType">
            <el-select v-model="queryParams.userType" class="!w-160px" clearable :placeholder="t('sys.oauth2.token.rules', 'userType')" @change="handleQuery">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.USER_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </ElFormItem>
          <ElFormItem :label="t('sys.oauth2.token', 'clientId')" prop="clientId" class="!w-240px">
            <el-input v-model="queryParams.clientId" :placeholder="t('sys.oauth2.token.rules', 'clientId')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem>
            <el-button type="primary" :disabled="queryLoading" icon="Search" @click="handleQuery">{{ t('action',
              'search') }}</el-button>
            <el-button icon="Refresh" :disabled="queryLoading" @click="resetQuery">{{ t('action', 'reset')
            }}</el-button>
          </ElFormItem>
        </ElForm>
      </template>
      <template #content>
        <el-table v-loading="queryLoading && systemConfig.loadingType === 'module'" :data="list" border>
          <el-table-column type="index" :label="t('common', 'index')" width="60" align="center" />
          <el-table-column :label="t('sys.oauth2.token', 'accessToken')" align="center" prop="accessToken" width="300" />
          <el-table-column :label="t('sys.oauth2.token', 'refreshToken')" align="center" prop="refreshToken" width="300" />
          <el-table-column :label="t('sys.oauth2.token', 'userId')" align="center" prop="userId" />
          <el-table-column :label="t('sys.oauth2.token', 'userType')" align="center" prop="userType">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.USER_TYPE" :value="scope.row.userType" />
            </template>
          </el-table-column>
          <el-table-column :label="t('sys.oauth2.token', 'clientId')" align="center" prop="clientId" />
          <el-table-column :label="t('sys.oauth2.token', 'expiresTime')" :formatter="dateTimeFormatter" align="center" prop="expiresTime" />
          <el-table-column :label="t('common', 'createTime')" :formatter="dateTimeFormatter" align="center" prop="create_time" />
          <el-table-column v-if="checkPermit(['system:oauth2-token:delete'])" prop="operate" :label="t('common', 'operate')" :width="computedOperateWidth(['system:oauth2-token:delete'])" align="center" fixed="right">
            <template #default="scope">
              <el-button v-if="checkPermit(['system:oauth2-token:delete'])" type="danger" icon="SwitchButton" @click="handleForceLogout(scope.row.accessToken)" />
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageIndex" :total="total" :disabled="queryLoading" @pagination="getList" />
      </template>
    </ContentFormTable>
  </PageCard>
</template>

<style scoped lang="scss">
.el-table {
  width: 100%;
  height: 100%;
}
</style>
